package com.frxuexi.park.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.room.ParkEntity
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.park.R
import com.frxuexi.park.adapter.ParkPreAdapter
import com.frxuexi.park.databinding.ActivityParkPreListBinding
import com.frxuexi.park.intent.ParkPreIntent
import com.frxuexi.park.viewmodel.ParkPreViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/park/ParkPreListActivity")
@AndroidEntryPoint
class ParkPreListActivity : BaseActivity<ActivityParkPreListBinding,ParkPreIntent,ParkPreViewModel>() {
    override val binding: ActivityParkPreListBinding by lazy { ActivityParkPreListBinding.inflate(layoutInflater) }
    override val vm: ParkPreViewModel by viewModels<ParkPreViewModel>()
    val parkPreAdapter by lazy { ParkPreAdapter() }
    var position=-1
    var page=0
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val entities = it.data as Collection<ParkEntity>
                                parkPreAdapter.apply {
                                    data.clear()
                                    addData(entities)
                                    notifyDataSetChanged()
                                }

                                binding.parkpreYy.setText("${entities.size}")
                                binding.parkpreSheng.setText("${100-entities.size}")
                            }

                            RequestType.DELETE->{
                                parkPreAdapter.notifyItemRemoved(position)
                                binding.parkpreYy.setText("${parkPreAdapter.data.size}")
                                binding.parkpreSheng.setText("${100-parkPreAdapter.data.size}")
                            }

                            else -> {}
                        }


                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {

        binding.parkpreAdd.setOnClickListener {
            ArouterManager.nav(this,"/park/ParkPreActivity")
        }

        binding.parkpreRv.apply {
            adapter=parkPreAdapter
            layoutManager=LinearLayoutManager(this@ParkPreListActivity)
        }


        parkPreAdapter.setOnItemLongClickListener { adapter, view, position ->
            this.position=position
            val entity = parkPreAdapter.data.get(position)
            vm.sendIntent(ParkPreIntent.deletePark(entity))
            parkPreAdapter.data.remove(entity)
            return@setOnItemLongClickListener true
        }

        binding.parkpreSv.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s.isNullOrEmpty()){
                    vm.sendIntent(ParkPreIntent.getParksByDate(DateTimeUtils.getDate(System.currentTimeMillis())))
                }else {
                    vm.sendIntent(ParkPreIntent.getParksByDateOrNum(s.toString()))
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })

        parkPreAdapter.addChildClickViewIds(R.id.parkpre_update)
        parkPreAdapter.setOnItemChildClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/park/ParkPreActivity",parkPreAdapter.data.get(position).id!!.toInt())
        }

        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=0
                vm.sendIntent(ParkPreIntent.getParks(5,page))
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                vm.sendIntent(ParkPreIntent.getParks(5,page))
                binding.smart.finishLoadMore()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        val sv = binding.parkpreSv.text.toString()
        if(sv.isNullOrEmpty()){
//            vm.sendIntent(ParkPreIntent.getParksByDate(DateTimeUtils.getDate(System.currentTimeMillis())))
            vm.sendIntent(ParkPreIntent.getParks(5,page))
        }else {
            vm.sendIntent(ParkPreIntent.getParksByDateOrNum(sv.toString()))
        }
    }

}